﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.Core.Configuration;
using Telerik.Web.UI;

namespace Medianamik.UI.ContentManagement
{
    public partial class PublicationList : BasePage
    {
        private static readonly bool _enableExtensionOfApprobation = ((MedianamikConfigSection)ConfigurationManager.GetSection(ConfigurationSectionName.MEDIANAMIK)).EnableExtensionOfApprobation;
        protected static IEnumerable<Guid> RootNodeIds
        {
            get 
            {
                return NodeManager.GetNodesByType<MedianamikModule>(true).Where(n => n.ShowInPublishing).Select(n => n.ID);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack )
            {
                GridNodes.DataBind();
            }
        }

        protected void btnPublish_Click(object sender, EventArgs e )
        {
            var publishNodeIds = GridNodes.AllChildren()
                .OfType<CheckBox>()
                .Where(c => c.ID == "chkPublish" && c.Checked )
                .Select(c => new Guid(c.Attributes["value"]));
            
            NodeManager.PublishNodes(publishNodeIds, MedianamikContext.Current.EditionCulture, DateTime.Now, null);
            GridNodes.Rebind();

        }

        protected void btnReject_Click(object sender, EventArgs e)
        {
            var rejectNodeIds = GridNodes.AllChildren()
                .OfType<CheckBox>()
                .Where(c => c.ID == "chkPublish" && c.Checked)
                .Select(c => new Guid(c.Attributes["value"]))
                .ToList();

           rejectNodeIds.ForEach(id=> NodeManager.RevertNode(id, MedianamikContext.Current.EditionCulture));
           GridNodes.Rebind();

        }

        protected void GridNodes_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType != GridItemType.Item && e.Item.ItemType != GridItemType.AlternatingItem)
            {
                return;
            }
       
        }

        protected void GridNodes_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            IEnumerable<Node> nodes;
            if (!_enableExtensionOfApprobation)
            {
                nodes = NodeManager.GetModifiedNodes(MedianamikContext.Current.EditionCulture,
                                                     MedianamikTypes.BaseType,
                                                     MedianamikModules.ContentManagement);
            }
            else
            {
                //TODO: Yannick est la-dessus
                //nodes = NodeManager.GetModifiedNodes(MedianamikContext.Current.EditionCulture,
                //                                     MedianamikTypes.BaseType,
                //                                     RootNodeIds);
                nodes = null;
            }
            var moduleId = MedianamikModules.ContentManagement ; 
            var parentNodes = NodeManager.GetNodes(nodes.Where(n=>n.ParentID.HasValue && n.ParentID.Value != moduleId)
                .Select(n=>n.ParentID.Value).Distinct().ToArray() );



            var datasource = from n in nodes
                             let parentName = parentNodes.Any(pn => pn.ID.Equals(n.ParentID))
                                     ? parentNodes.First(pn => pn.ID.Equals(n.ParentID)).DisplayName
                                     : "Racine"
                             select new
                             {
                                 ID = n.ID,
                                 Node = n,
                                 ParentName = parentName,
                                 EditedOn = n.EditedOn,
                                 EditedBy = n.EditedBy
                             };

            GridNodes.DataSource = datasource;
        }

        protected void GridNodes_ItemCommand(object source, GridCommandEventArgs e)
        {
           
        }

        protected void LockInfoTooltip_LockDeleted(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }

        protected void LockLink_RequestFailed(object sender, CommandEventArgs e)
        {
            GridNodes.Rebind();
            LockInfoTooltip.ObjectId = new Guid((string)e.CommandArgument);
            LockInfoTooltip.DataBind();
            LockInfoTooltip.Show();
        }
        protected override void OnCultureChanged(CultureChangedEventArgs e)
        {
            base.OnCultureChanged(e);
            GridNodes.Rebind();
        }
    }
}
